package org.example.demo02;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequestMapping("/param")
@RestController
public class ParamController {
    @RequestMapping("/p1")
    public String p1(String name) {
        return "接收到参数, name:" + name;
    }

    @RequestMapping("/p4")

    public String p4(String name,int age) {
        return "接受到参数, age: " + age + ",name:" + name;
    }

    //另一种写法
    @RequestMapping("/p41")
    public String p41 (HttpServletRequest request) {
        String name = request.getParameter("name");
        Integer age = Integer.valueOf(request.getParameter("age"));
        return "接收到参数,age:" + age + "name" + name;
    }
    @RequestMapping("/p5")
    public String p5(String name, int age, String gender) {
        return "接收到参数, age:" + age + ", name:" + name + ", gender :" + gender;
    }
    @RequestMapping("/p6")
    public String p6(User user) {
        return "user" + user;
    }
    //给参数重命名
    @RequestMapping("/p7")
    public String p7(@RequestParam(value = "username",required = false) String name) {
        return "接收到参数, name :" + name;
    }
    @RequestMapping("/p8")
    public String p8(String[] arr) {
        return "arr:" + List.of(arr);
    }
    @RequestMapping("/p9")
    public String p9(@RequestParam List<String> list) {
        return "list:" + list;
    }
    @RequestMapping("/p10")
    public String p10(@RequestBody User user) {
        return "user" + user;
    }
    @RequestMapping("/article/{articleId}")
    //获取url中的参数
    public String p11(@PathVariable String articleId) {
        return "articleId:" + articleId;
    }
@RequestMapping("/article/{articleId}/{name}")
    public String p12(@PathVariable("articleId") String id, @PathVariable String name) {
        return "articleId:" + id + ", name:" + name;
    }

//    //上传文件的 注释
    @RequestMapping("/p13")
    public String p13(@RequestPart("fileName") MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return "文件名称:" + file.getOriginalFilename();
    }

}
